package com.example.spring_seatreservation.controller;

import com.example.spring_seatreservation.Bean.R;
import com.example.spring_seatreservation.entity.Library;
import com.example.spring_seatreservation.mapper.FloorMapper;
import com.example.spring_seatreservation.mapper.LibraryMapper;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/library")
@AllArgsConstructor
public class LibraryController {

    private final LibraryMapper libraryMapper;
    private final FloorMapper floorMapper;

    @GetMapping("getLibraryList")
    public List<Library> getLibraryList(){
        return libraryMapper.selectList(null);
    }

    @PostMapping("delLib")
    public Map<String, Object> delLib(@RequestBody Library library){
        int floorNum = floorMapper.selectFloorByLibId(library.getId());
        if (floorNum > 0) {
            return new R().bad("清先删除楼层").builder();
        }
        libraryMapper.deleteById(library.getId());
        return new R().ok().builder();
    }

    @PostMapping("submit")
    public Map<String, Object> submit(@RequestBody Library library){
        if (library.getId() == null) {
            libraryMapper.insert(library);
        } else {
            libraryMapper.updateById(library);
        }
        return new R().ok().builder();
    }
}
